#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>
#include <sys/time.h>
#include<signal.h>

typedef struct
{
	double sec;
	double usec;
}mytime;

void set_sys_time();

void print_err(char *str, int line, int err_no);
void send_server_current_time(int socket_cli_fd);
struct timeval start,end;
mytime start_d,end_d,temp;


int main()
{
	
	int value = 1;
	char recvbuf[1024] = "";

	
	set_sys_time();

	
	/* configure server ip and port */
	int sockSrv = socket(AF_INET, SOCK_STREAM, 0);
	setsockopt(sockSrv,SOL_SOCKET,SO_REUSEADDR,&value,sizeof(value));

	
	struct sockaddr_in addrSrv;
	addrSrv.sin_family = AF_INET;
	addrSrv.sin_addr.s_addr = htonl(INADDR_ANY); 
	addrSrv.sin_port = htons(5050);

	int rest = bind(sockSrv, (const struct sockaddr*)&addrSrv, sizeof(struct sockaddr_in));
	if(rest == -1)
		printf("what happen? bind error!\n");
	listen(sockSrv,10);
	signal(SIGPIPE, SIG_IGN);//


	/* store client info */
	struct sockaddr_in addrClient;
	int len = sizeof(struct sockaddr_in);
	int sockConn = 0;
	


	
	//setsockopt(sockConn, SOL_SOCKET, MSG_NOSIGNAL, &value, sizeof(value));
	//setsockopt(sockSrv, SOL_SOCKET, MSG_NOSIGNAL, &value, sizeof(value));


	while (1)/* get the time of client then send the server curent time to client */
	{	
		sockConn = accept(sockSrv, NULL, NULL);
		int ret = recv(sockConn, recvbuf, sizeof(recvbuf),0);
		if (-1 == ret) {
			print_err("recv failed",__LINE__,errno);
		}
		else
		{
			char *p  = recvbuf;
			mytime* pp = (mytime *)p;
			printf("Client 发送过来的时间秒数是：%lf,毫秒数是：%lf\n",pp->sec,pp->usec);
			send_server_current_time(sockConn);
		}
			
	}

	close(sockConn);
	close(sockSrv);

	return 0;
}

void send_server_current_time(int socket_cli_fd)
{
	char sendbuf[1024] = "";
	bzero(&end, sizeof(end));
	gettimeofday(&end, NULL);
	end_d.sec = end.tv_sec;
	end_d.usec = end.tv_usec;
	
	printf("Server现在的时间秒数是：%ld  ,  %lf毫秒数是：%ld , %lf\n",end.tv_sec,end_d.sec,end.tv_usec,end_d.usec);
	char *p = sendbuf;
	memcpy(p, (char *)&end_d, sizeof(end_d));
	//long int ti =  (end.tv_sec * 1000000 + end.tv_usec);
	//printf("Server 发送过去的时间 : %ld us\n",ti);
	
	int ret = send(socket_cli_fd,sendbuf,sizeof(sendbuf),0);
	if ( -1 == ret) {
		print_err("send failed", __LINE__, errno);
	}else{
	
		printf("Server  发送过去的时间秒数是：%lf,毫秒数是：%lf\n",end_d.sec,end_d.usec);
		printf("************************************************************\n\n");
		/*char *p = sendbuf;
		struct timeval* pp = (struct timeval*)p;
		printf("Server 2   发送过去的时间秒数是：%ld,毫秒数是：%ld\n",pp->tv_sec,pp->tv_usec); */
	}
}


void print_err(char *str, int line, int err_no) {
	printf("%d, %s :%s\n",line,str,strerror(err_no));
	_exit(-1);
}


void set_sys_time()
{
	/* set the system time to a specify */
	struct timeval stime;
	//printf("sizeof time val : %d", sizeof(struct stimeval));
	stime.tv_sec =1599719331;
	stime.tv_usec =123;
	if(settimeofday(&stime,NULL) == 0){  // set success
		sleep(1);
		gettimeofday(&stime, NULL);
		temp.sec = stime.tv_sec;
		temp.usec = stime.tv_usec;
		printf("Serverset time success\n");
		printf("Server现在的时间秒数是：%ld  ,  %lf毫秒数是：%ld , %lf\n现在的时间是:",stime.tv_sec,temp.sec,stime.tv_usec,temp.usec);
		fflush(stdout);
		system("date");
		printf("***********************************************************************************\n\n");
	}
	
}
